﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TP2
{
    public partial class PaymentForm : Form
    {
        int id_client;
        int credit_sum;
        LegalClientTypeClass selectMass;
        public PaymentForm(int idClient)
        {
            id_client = idClient;
            InitializeComponent();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int id_select_client = Convert.ToInt32(selectMass.idClient[index]);
                int trans_sum = Convert.ToInt32(textBoxTranslete.Text);
                int deposit = Convert.ToInt32(labelPaimentDeposit.Text);
                LegalClientTypeClass legalClientMass = new LegalClientTypeClass();
                DBWorkClass dbWork = new DBWorkClass();
                legalClientMass = dbWork.SelectFromLegalClient();
                int i = 0;
                string title_sender = "";
                string inn_sender = "";
                while (i < legalClientMass.idClient.Count)
                {
                    if (id_client == Convert.ToInt32(legalClientMass.idClient[i]))
                    {
                        title_sender = legalClientMass.Title[i].ToString();
                        inn_sender = legalClientMass.INN[i].ToString();
                        break;
                    }
                    i++;
                }
                int summ_select_client = dbWork.SelectFromPayments(id_select_client);
                if (trans_sum <= deposit)
                {
                    int new_summ = summ_select_client + trans_sum;
                    dbWork.UpdateFromPayments(id_select_client, new_summ);
                    int summ = Convert.ToInt32(labelPaimentDeposit.Text) - trans_sum;
                    dbWork.UpdateFromPayments(id_client, summ);
                    dbWork.InsertFromHistory(title_sender, inn_sender, dataGridView1[0, index].Value.ToString(), dataGridView1[1, index].Value.ToString(), trans_sum);
                    this.RefreshInfo();
                }
                else { MessageBox.Show("у вас на счёте нет такой суммы."); }
                MessageBox.Show("операция произведена.");
            }
            catch (FormatException) { MessageBox.Show("ошибка во входных данных!"); }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RefreshInfo()
        {
            DBWorkClass dbWork = new DBWorkClass();
            selectMass = new LegalClientTypeClass();
            selectMass = dbWork.SelectFromLegalClient();
            int j = 0;
            if (selectMass.idClient.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
                dataGridView1[3, 0].Value = "";

            }
            else { dataGridView1.RowCount = selectMass.idClient.Count - 1; }
            int i = 0;
            while (j < selectMass.idClient.Count)
            {
                if (Convert.ToInt32(selectMass.idClient[j]) != id_client)
                {
                    dataGridView1[0, i].Value = selectMass.Title[j].ToString();
                    dataGridView1[1, i].Value = selectMass.INN[j].ToString();
                    dataGridView1[2, i].Value = selectMass.Capital[j].ToString();
                    dataGridView1[3, i].Value = selectMass.Type[j].ToString();
                    dataGridView1[4, i].Value = selectMass.Founders[j].ToString();
                    i++;
                }
                j++;

            }
            credit_sum = dbWork.SelectFromPayments(id_client);
            labelPaimentDeposit.Text = credit_sum.ToString();
 
        }
        
        private void PaymentForm_Load(object sender, EventArgs e)
        {
            this.RefreshInfo();
        }

        private void buttonHistory_Click(object sender, EventArgs e)
        {
            HystoryTranslationForm historyForm = new HystoryTranslationForm(id_client);
            historyForm.Show();
        }
    }
}
